//
//  communityCollectionViewController.m
//  17DianYing
//
//  Created by apple on 15/9/24.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "communityCollectionViewController.h"

#import "communityManager.h"

#import "UICollectionViewCell+fun.h"


#import "MBProgressHUD.h"

@interface communityCollectionViewController ()<MBProgressHUDDelegate>

{
    MBProgressHUD *HUD;
    long long expectedLength;
    long long currentLength;
}


@property(nonatomic,strong)communityManager *communityM;
@property(nonatomic,strong)NSMutableArray *community_arr;

@end

@implementation communityCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    
    self.communityM = [communityManager share];
    
    [super viewDidLoad];
    
    [self getcommunity];
    
    self.community_arr = [NSMutableArray array];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes

    
    // Do any additional setup after loading the view.
}

-(void)getcommunity
{
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    
    HUD.dimBackground = YES;
    
    
    HUD.delegate = self;
    
    [HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];

    
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *usertoken = [user objectForKey:@"usertoken"];
    
    [self.communityM getSelfWithToken:usertoken BlockHoudle:^(NSDictionary *dic) {
        
        NSString *str_status = [NSString stringWithFormat:@"%@",dic[@"status"]];
        
        if ([str_status isEqualToString:@"0"])
        {
            NSArray *arr = dic[@"films"];

            
            for (NSDictionary *dicList in arr)
            {
                [self.community_arr addObject:dicList];
            }
            [HUD hide:YES];
            [self.collectionView reloadData];
            

        }
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    
   
    
    return self.community_arr.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SelfCommunity" forIndexPath:indexPath];
    
    NSDictionary *dic1 = self.community_arr[indexPath.row];
    
    [cell setCellInfoWithNSDictionary:dic1];
    

    
     return cell;
}
- (void)myTask {
    // Do something usefull in here instead of sleeping ...
        sleep(3);
//    [HUD hide:YES];
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic1 = self.community_arr[indexPath.row];
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"CommunityDetalls"];
    
    [self.navigationController pushViewController:vc animated:YES];
    
    [vc setValue:dic1[@"filmId"] forKey:@"filmId"];
    
}


#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
